<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .txt {
            width: 100px;
            text-align: center;
        }
        .txt.active {
            animation: rotate 1s linear infinite;
        }
        @keyframes rotate {
            0% {
                transform: rotate(0deg);
            }
            100% {
                transform: rotate(360deg);
            }
        }
    </style>
</head>
<body>
    <div class="txt">hello</div>
    <button id="btn">转圈</button>

    <script>

        var txt = document.querySelector('.txt');
        var btn = document.querySelector('#btn');

        var isRotating = false;

        // 匿名函数
        btn.addEventListener('click', function () {
            // if (!isRotating) {
            //     txt.setAttribute('class', 'txt active');
            //     btn.innerText = '停止';
            // } else {
            //     txt.setAttribute('class', 'txt');
            //     btn.innerText = '转圈';
            // }
            txt.setAttribute('class', 'txt ' + (!isRotating ? 'active' : ''));
            btn.innerText = !isRotating ? '停止' : '转圈';
            isRotating = !isRotating;
        });

    </script>
</body>
</html>